package com.lwl.service.impl.sds;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.dao.sds.TSdsRecordDetailMapper;
import com.lwl.entity.*;
import com.lwl.entity.mht.TQuestionMht;
import com.lwl.entity.sds.TSdsQuestion;
import com.lwl.entity.sds.TSdsRecordDetail;
import com.lwl.service.sds.TSdsQuestionService;
import com.lwl.dao.sds.TSdsQuestionMapper;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * (TSdsQuestion)表服务实现类
 *
 * @author Lei
 * @since 2022-11-09 21:22:59
 */
@Service
public class TSdsQuestionServiceImpl implements TSdsQuestionService {
   @Autowired
   private TSdsQuestionMapper tSdsQuestionDao;
   @Autowired
   private TSdsRecordDetailMapper tSdsRecordDetailMapper;

   @Override
   public PageInfo queryAll(MyPage myPage, TQuestionMht tQuestionMht) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      List<TSdsQuestion> tSdsQuestions = tSdsQuestionDao.selectAll();
      PageInfo pageInfo = new PageInfo(tSdsQuestions);
      return pageInfo;
   }

   @Override
   public boolean insertQuestion(TSdsQuestion tSdsQuestion) {
      int insert = -1;
      if (tSdsQuestion.getId()==null){
         insert = tSdsQuestionDao.insert(tSdsQuestion);
      }else {
         insert = tSdsQuestionDao.updateByPrimaryKeySelective(tSdsQuestion);
      }
      return insert>0?true:false;
   }


   @Override
   public boolean dealFile(MultipartFile file) {
      // 如果试题已经存在就不让再导入了
      int i = tSdsQuestionDao.selectCount(null);
      if(i>0){
         return false;
      }else {
         InputStream is = null;
         try {
            is = file.getInputStream();
         } catch (IOException e) {
            e.printStackTrace();
         }
         //1.进行读取数据，TFpaSub是我的pojo类，
         //2.new FileListener(fileService)这个是监听器，主要用来读取数据的
         //3.特别注意的是fileService这个service，我上面有注入进去 @Autowired/@Resource，切记不要new会报错
         //EasyExcel.read(is, TFpaSub.class, new FileListener(fileService)).sheet("FPA性格测试").doRead();
         EasyExcel.read(is, TQuestionMht.class, new AnalysisEventListener<TSdsQuestion>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 100;
            /**
             * 临时存储
             */
            private List<TSdsQuestion> cachedDataList = new ArrayList<>();

            @Override
            public void invoke(TSdsQuestion tSdsQuestion, AnalysisContext analysisContext) {
               cachedDataList.add(tSdsQuestion);
               if (cachedDataList.size() >= BATCH_COUNT) {
                  saveData();
                  // 存储完成清理 list
                  cachedDataList = new ArrayList<>();
               }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
               saveData();
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
//            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
               save(cachedDataList);
            }
         }).sheet("抑郁自评量表").doRead();
      }
      return false;
   }

   @Override
   public void save(List<TSdsQuestion> list) {
      tSdsQuestionDao.insertFtaSubjects(list);
   }

   @Override
   public List<Integer> deleteMoreQuestion(Integer[] dataId) {
      List<Integer> delList = new ArrayList<>();
      List<Integer> noDelList = new ArrayList<>();
      for (Integer integer : dataId) {
         TSdsRecordDetail tSdsRecordDetail = new TSdsRecordDetail();
         tSdsRecordDetail.setSdsno(integer);
         int i = tSdsRecordDetailMapper.selectCount(tSdsRecordDetail);
         if (i==0){
            delList.add(integer);
         }else {
            noDelList.add(integer);
         }
      }
      if (delList.size()>0){
        int i = tSdsQuestionDao.deleteMoreQuestion(delList);
      }
      return noDelList;
   }

   @Override
   public List<TSdsRecordDetail> queryTMhtRecordDetail(Integer id) {
      List<TSdsRecordDetail> tMhtRecordDetailList = tSdsQuestionDao.queryTSdsRecordDetail(id);
      return tMhtRecordDetailList;
   }

   @Override
   public boolean deleteQuestion(Integer id) {
      int delete = tSdsQuestionDao.deleteByPrimaryKey(id);
      return delete>0?true:false;
   }

   @Override
   public PageInfo getAll(MyPage myPage) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      return new PageInfo(tSdsQuestionDao.selectAll());
   }
}
